// var r;
// var AL_GAME_HUA_i18nMgr = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_i18nMgr");
// var AL_GAME_HUA_AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_ButtonEnabled = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_ButtonEnabled");
// var AL_GAME_HUA_Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
// var AL_GAME_HUA_PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
// var AL_GAME_HUA_EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
// var AL_GAME_HUA_WelfareConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareConfig");
// var AL_GAME_HUA_WelfareData = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareData");
// var g = cc._decorator;
// var v = g.ccclass;
// var m = g.property;
// var b = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.layout = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.start = function () {
//         this.chushihua_ui();
//     };
//     t.prototype.chushihua_ui = function () {
//         var e = this.layout.children;
//         var t = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance();
//         for (var n = (AL_GAME_HUA_WelfareData.WelfareData.getInstance(), 0); n < e.length; n++) {
//             var o = e[n];
//             var r = cc.find("button", o);
//             var a = r.getComponent(AL_GAME_HUA_ButtonEnabled.default);
//             var c = cc.find("label", r).getComponent(cc.Label);
//             var i = cc.find("reward", o).getComponent(cc.Label);
//             var s = t.huoqu_fuli_peizhi_genju_id(n).rewardType[0].num;
//             i.string = "x" + s;
//             o.button = a;
//             o.buttonLabel = c;
//             this.shuaxin_anniu(n);
//         }
//     };
//     t.prototype.shuaxin_anniu = function (e) {
//         var t = this.layout.children[e];
//         var n = t.button;
//         var o = t.buttonLabel;
//         var r = AL_GAME_HUA_WelfareData.WelfareData.getInstance();
//         var a = r.inviteList.length;
//         var c = r.huoqu_shifou_yi_jieshou_yaoqing_jiangli(e);
//         var s = r.huoqu_yaoqing_guize();
//         var f = a >= s[e];
//         var l = !c && f;
//         n.shezhi_anniu_jiaohuxing(l);
//         o.string = c
//             ? AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("已领取")
//             : f
//             ? AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("领取")
//             : AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("邀请") + a + "/" + s[e];
//     };
//     t.prototype.chuli_lingqu = function (e) {
//         var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance();
//         var n = t.huoqu_shifou_yi_jieshou_yaoqing_jiangli(e);
//         var o = t.huoqu_yaoqing_guize();
//         var r = t.inviteList.length >= o[e];
//         if (!n && r) {
//             t.jieshou_yaoqing_jiangli(e);
//             this.shuaxin_anniu(e);
//             var a = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(e);
//             AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
//                 rewards: a.rewardType
//             });
//             AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                 source: AL_GAME_HUA_EventConfig.AssetsSource.welfare_invite,
//                 action: AL_GAME_HUA_EventConfig.AssetsAction.get,
//                 goodsList: a.rewardType.map(function (e) {
//                     return {
//                         goods: e.goodsId,
//                         num: e.num
//                     };
//                 })
//             });
//         }
//     };
//     t.prototype.handle_button_callback = function (e, t) {
//         switch (t) {
//             case "close":
//                 this.close();
//                 break;
//             case "recieve0":
//                 this.chuli_lingqu(0);
//                 break;
//             case "recieve1":
//                 this.chuli_lingqu(1);
//                 break;
//             case "recieve2":
//                 this.chuli_lingqu(2);
//                 break;
//             case "invite":
//                 AL_GAME_HUA_Common.Common.share({
//                     location: AL_GAME_HUA_Define.Define.SHARE_LOCATION.INVITE,
//                     type: 2
//                 });
//         }
//     };
//     __decorate([m(cc.Node)], t.prototype, "layout", void 0);
//     return __decorate([v], t);
// })(AL_GAME_HUA_PopupSelf.default);
// exports.default = b;


const AL_GAME_HUA_i18nMgr = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_i18nMgr");
const AL_GAME_HUA_AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
const AL_GAME_HUA_ButtonEnabled = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_ButtonEnabled");
const AL_GAME_HUA_Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
const AL_GAME_HUA_Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
const AL_GAME_HUA_PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
const AL_GAME_HUA_EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
const AL_GAME_HUA_WelfareConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareConfig");
const AL_GAME_HUA_WelfareData = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareData");

const { ccclass, property } = cc._decorator;

/**
 * Welfare Popup Class
 */
@ccclass
class WelfarePopup extends AL_GAME_HUA_PopupSelf.default {
    @property(cc.Node)
    layout = null;

    start() {
        this.chushihua_ui();
    }

    /** 
     * Initialize UI elements 
     */
    chushihua_ui() {
        const children = this.layout.children;
        const welfareConfigInstance = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance();
        
        AL_GAME_HUA_WelfareData.WelfareData.getInstance();

        children.forEach((child, index) => {
            const button_Node = cc.find("button", child);
            const buttonComponent = button_Node.getComponent(AL_GAME_HUA_ButtonEnabled.default);
            const labelComponent = cc.find("label", button_Node).getComponent(cc.Label);
            const rewardLabel = cc.find("reward", child).getComponent(cc.Label);
            const rewardNum = welfareConfigInstance.huoqu_fuli_peizhi_genju_id(index).rewardType[0].num;

            rewardLabel.string = `x${rewardNum}`;
            child.button = buttonComponent;
            child.buttonLabel = labelComponent;

            this.shuaxin_anniu(index);
        });
    }

    /**
     * Refresh button status and label
     * @param {number} index - Index of the button to refresh
     */
    shuaxin_anniu(index) {
        const child = this.layout.children[index];
        const buttonComponent = child.button;
        const labelComponent = child.buttonLabel;
        const welfareDataInstance = AL_GAME_HUA_WelfareData.WelfareData.getInstance();
        const inviteCount = welfareDataInstance.inviteList.length;
        const inviteReceived = welfareDataInstance.huoqu_shifou_yi_jieshou_yaoqing_jiangli(index);
        const inviteRule = welfareDataInstance.huoqu_yaoqing_guize();
        const canReceive = inviteCount >= inviteRule[index];
        const interactable = !inviteReceived && canReceive;

        buttonComponent.shezhi_anniu_jiaohuxing(interactable);
        labelComponent.string = inviteReceived 
            ? AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("已领取")
            : canReceive
                ? AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("领取")
                : `${AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("邀请")}${inviteCount}/${inviteRule[index]}`;
    }

    /**
     * Handle receiving rewards
     * @param {number} index - Index of the reward to receive
     */
    chuli_lingqu(index) {
        const welfareDataInstance = AL_GAME_HUA_WelfareData.WelfareData.getInstance();
        const inviteReceived = welfareDataInstance.huoqu_shifou_yi_jieshou_yaoqing_jiangli(index);
        const inviteRule = welfareDataInstance.huoqu_yaoqing_guize();
        const canReceive = welfareDataInstance.inviteList.length >= inviteRule[index];

        if (!inviteReceived && canReceive) {
            welfareDataInstance.jieshou_yaoqing_jiangli(index);
            this.shuaxin_anniu(index);

            const welfareConfig = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(index);
            AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
                rewards: welfareConfig.rewardType
            });

            AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
                source: AL_GAME_HUA_EventConfig.AssetsSource.welfare_invite,
                action: AL_GAME_HUA_EventConfig.AssetsAction.get,
                goodsList: welfareConfig.rewardType.map(reward => ({
                    goods: reward.goodsId,
                    num: reward.num
                }))
            });
        }
    }

    /**
     * Handle button callback
     * @param {cc.Event.EventCustom} event - The event object
     * @param {string} action - The action name
     */
    handle_button_callback(event, action) {
        switch (action) {
            case "close":
                this.close();
                break;
            case "recieve0":
                this.chuli_lingqu(0);
                break;
            case "recieve1":
                this.chuli_lingqu(1);
                break;
            case "recieve2":
                this.chuli_lingqu(2);
                break;
            case "invite":
                AL_GAME_HUA_Common.Common.share({
                    location: AL_GAME_HUA_Define.Define.SHARE_LOCATION.INVITE,
                    type: 2
                });
        }
    }
}

exports.default = WelfarePopup;
